// pages/add/add.js
var http = require('../../utils/http');
var api = require('../../utils/api');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name: "",
        idCard: "",
        errorFlag: false,
        errorIdCardFlag: false,
        activeNames: [],
        tineLong: 0,
        mineLong: 0,
        timer: '',
        data: [{
                icon: "https://www.seemoreopt.com/api/getFile/97",
                text1: "户外活动时长",
                text2: "建议每日>2小时",
                timeLong: 0,
                rtype: 1,
                disabled: false,
                columns: [{
                        text: '>6',
                        value: '360'
                    },
                    {
                        text: '5.0',
                        value: '300'
                    },
                    {
                        text: '4.5',
                        value: '270'
                    },
                    {
                        text: '4.0',
                        value: '240'
                    },
                    {
                        text: '3.5',
                        value: '210'
                    },
                    {
                        text: '3.0',
                        value: '180'
                    },
                    {
                        text: '0',
                        value: '0'
                    },
                    {
                        text: '0.5',
                        value: '30'
                    },
                    {
                        text: '1.0',
                        value: '60'
                    },
                    {
                        text: '1.5',
                        value: '90'
                    },
                    {
                        text: '2.0',
                        value: '120'
                    },
                    {
                        text: '2.5',
                        value: '150'
                    },
                ],
                columns2:[  {
                    values: [
                        {
                            text: '>6',
                            value: '360'
                        },
                        {
                            text: '5.0',
                            value: '300'
                        },
                        {
                            text: '4.5',
                            value: '270'
                        },
                        {
                            text: '4.0',
                            value: '240'
                        },
                        {
                            text: '3.5',
                            value: '210'
                        },
                        {
                            text: '3.0',
                            value: '180'
                        },
                        {
                            text: '0',
                            value: '0'
                        },
                        {
                            text: '0.5',
                            value: '30'
                        },
                        {
                            text: '1.0',
                            value: '60'
                        },
                        {
                            text: '1.5',
                            value: '90'
                        },
                        {
                            text: '2.0',
                            value: '120'
                        },
                        {
                            text: '2.5',
                            value: '150'
                        },
                    ],
                    defaultIndex: 5
                },
                {
                    values: ['小时']
                },]
            },
            {
                icon: "https://www.seemoreopt.com/api/getFile/98",
                text1: "电子屏观看总时长",
                text2: "建议每次<20分钟",
                timeLong: 0,
                rtype: 2,
                disabled: false,
                columns: [{
                        text: '>120',
                        value: '120'
                    },
                    {
                        text: '120',
                        value: '120'
                    },
                    {
                        text: '110',
                        value: '110'
                    },
                    {
                        text: '0',
                        value: '0'
                    },
                    {
                        text: '10',
                        value: '10'
                    },
                    {
                        text: '20',
                        value: '20'
                    },
                    {
                        text: '30',
                        value: '30'
                    },
                    {
                        text: '40',
                        value: '40'
                    },
                ],
                columns2:[  {
                    values: [
                        {
                            text: '>120',
                            value: '120'
                        },
                        {
                            text: '120',
                            value: '120'
                        },
                        {
                            text: '110',
                            value: '110'
                        },
                        {
                            text: '0',
                            value: '0'
                        },
                        {
                            text: '10',
                            value: '10'
                        },
                        {
                            text: '20',
                            value: '20'
                        },
                        {
                            text: '30',
                            value: '30'
                        },
                        {
                            text: '40',
                            value: '40'
                        },
                    ],
                    defaultIndex: 5
                },
                {
                    values: ['分钟']
                },]
            },
            {
                icon: "https://www.seemoreopt.com/api/getFile/99",
                text1: "佩戴OK镜/近视防控镜",
                text2: "建议每日佩戴",
                timeLong: 0,
                rtype: 3,
                disabled: true,
                columns: [],
                columns2:[]
            },
        ],
        child_id: "",
        todayDate: '',
        childInfo: {},
        child: [],
        showPopup: false,
        showSelectPopup: false,
        radioId: "",
        telephone:""
    },
    onCloseSelectPopup() {
        this.setData({
            showSelectPopup: false
        })
        setTimeout(() => {
            this.init()
        }, 500);
    },
    //获取当前时间
    getNowTime() {
        var date = new Date();
        //年 getFullYear()：四位数字返回年份
        var year = date.getFullYear(); //getFullYear()代替getYear()
        //月 getMonth()：0 ~ 11
        var month = date.getMonth() + 1;
        //日 getDate()：(1 ~ 31)
        var day = date.getDate();
        // //时 getHours()：(0 ~ 23)
        // var hour = date.getHours();
        // //分 getMinutes()： (0 ~ 59)
        // var minute = date.getMinutes();
        // //秒 getSeconds()：(0 ~ 59)
        // var second = date.getSeconds();
        var a = new Array("日", "一", "二", "三", "四", "五", "六");
        var week = new Date().getDay();
        var str = "星期" + a[week];
        var time = '今天：' + year + '.' + this.addZero(month) + '.' + this.addZero(day) + ' ' + str;
        var todayDate = year + '-' + this.addZero(month) + '-' + this.addZero(day)
        this.setData({
            todayDate: todayDate
        })
        return time;
    },
    //小于10的拼接上0字符串
    addZero(s) {
        return s < 10 ? ('0' + s) : s;
    },
    onChange(event) {
        this.setData({
            activeNames: event.detail,
        });
    },
    onChangePicker(event) {
        console.log(event);
        const rtype = event.currentTarget.dataset.rtype
        const {
            picker,
            value,
            index
        } = event.detail;
        if (rtype == 1) {
            this.setData({
                tineLong: value[0].value
            })
        } else {
            this.setData({
                mineLong: value[0].text
            })
        }

    },
    jumpSignData() {
        wx.navigateTo({
            url: '/pages/signData/index',
        })
    },
    /** 打卡 */
    submitSign(e) {
        const rtype = e.currentTarget.dataset.rtype
        console.log(this.data.tineLong);
        if (rtype == 1) {
            if (this.data.tineLong == 0) {
                // wx.showToast({
                //     icon: "success",
                //     image: '../../images/error.png',
                //     title: "请选择时长",
                //     mask: true
                // })
                return
            }
            http.postHttp(api.saveRecord, {
                    child_id: this.data.child_id,
                    rtype: rtype,
                    long_time: this.data.tineLong
                },
                (result) => {
                    console.log(result);
                    if (result.code == 0) {
                          wx.showToast({
                            title: '打卡成功',
                          })
                        this.getSignRecord(this.data.child_id)
                    }
                },
                (err) => {
                    console.log(err);
                },
            )

        }
        if (rtype == 2) {
            if (this.data.mineLong == 0) {
                // wx.showToast({
                //     icon: "success",
                //     image: '../../images/error.png',
                //     title: "请选择时长",
                //     mask: true
                // })
                return
            }
            http.postHttp(api.saveRecord, {
                    child_id: this.data.child_id,
                    rtype: rtype,
                    long_time: this.data.mineLong
                },
                (result) => {
                    if (result.code == 0) {
                        wx.showToast({
                            title: '打卡成功',
                          })
                        this.getSignRecord(this.data.child_id)
                    }
                },
                (err) => {
                    console.log(err);
                },
            )
        }
        if (rtype == 3) {
            if (this.data.data[2].long_time > 0) {
                return
            }
            http.postHttp(api.saveRecord, {
                    child_id: this.data.child_id,
                    rtype: rtype,
                    long_time: 1
                },
                (result) => {
                    if (result.code == 0) {
                       
                        wx.showToast({
                          title: '打卡成功',
                        })
                        this.getSignRecord(this.data.child_id)
                    }
                },
                (err) => {
                    console.log(err);
                },
            )
        }
        this.setData({
            tineLong: 0,
            mineLong: 0
        })
    },
    getSignRecord(child_id) {
        function toHourMinute(minutes) {
            return ((minutes / 60));
        }
        http.postHttp(api.getSignRecord + child_id, {
                date: this.data.todayDate
            },
            (result) => {
                console.log(result);
                if (result.code == 0) {
                    result.data.records.forEach(ele => {
                        if (ele.id == 1) {
                            ele.long_time = toHourMinute(ele.long_time)
                            ele.icon = 'https://pcsys.admin.ybc365.com/72efb566-f6ab-4d3e-b0dc-e0fc3adc6947.png'
                            ele.columns =  [{
                                    text: '>6',
                                    value: '360'
                                },
                                {
                                    text: '5.0',
                                    value: '300'
                                },
                                {
                                    text: '4.5',
                                    value: '270'
                                },
                                {
                                    text: '4.0',
                                    value: '240'
                                },
                                {
                                    text: '3.5',
                                    value: '210'
                                },
                                {
                                    text: '3.0',
                                    value: '180'
                                },
                                {
                                    text: '0',
                                    value: '0'
                                },
                                {
                                    text: '0.5',
                                    value: '30'
                                },
                                {
                                    text: '1.0',
                                    value: '60'
                                },
                                {
                                    text: '1.5',
                                    value: '90'
                                },
                                {
                                    text: '2.0',
                                    value: '120'
                                },
                                {
                                    text: '2.5',
                                    value: '150'
                                },
                            ]
                            ele.columns2 = [  {
                                values: [
                                    {
                                        text: '>6',
                                        value: '360'
                                    },
                                    {
                                        text: '5.0',
                                        value: '300'
                                    },
                                    {
                                        text: '4.5',
                                        value: '270'
                                    },
                                    {
                                        text: '4.0',
                                        value: '240'
                                    },
                                    {
                                        text: '3.5',
                                        value: '210'
                                    },
                                    {
                                        text: '3.0',
                                        value: '180'
                                    },
                                    {
                                        text: '0',
                                        value: '0'
                                    },
                                    {
                                        text: '0.5',
                                        value: '30'
                                    },
                                    {
                                        text: '1.0',
                                        value: '60'
                                    },
                                    {
                                        text: '1.5',
                                        value: '90'
                                    },
                                    {
                                        text: '2.0',
                                        value: '120'
                                    },
                                    {
                                        text: '2.5',
                                        value: '150'
                                    },
                                ],
                                defaultIndex: 6
                            },
                            {
                                values: ['小时']
                            },]
                        }
                        if (ele.id == 2) {
                            ele.icon = 'https://pcsys.admin.ybc365.com/667f6e60-eab0-416a-9c36-6a0c340dda6f.png'
                            ele.columns = [{
                                    text: '>120',
                                    value: '120'
                                },
                                {
                                    text: '120',
                                    value: '120'
                                },
                                {
                                    text: '110',
                                    value: '110'
                                },
                                {
                                    text: '0',
                                    value: '0'
                                },
                                {
                                    text: '10',
                                    value: '10'
                                },
                                {
                                    text: '20',
                                    value: '20'
                                },
                                {
                                    text: '30',
                                    value: '30'
                                },
                                {
                                    text: '40',
                                    value: '40'
                                },
                            ]
                            ele.columns2 = [  {
                                values: [
                                    {
                                        text: '>120',
                                        value: '120'
                                    },
                                    {
                                        text: '120',
                                        value: '120'
                                    },
                                    {
                                        text: '110',
                                        value: '110'
                                    },
                                    {
                                        text: '0',
                                        value: '0'
                                    },
                                    {
                                        text: '10',
                                        value: '10'
                                    },
                                    {
                                        text: '20',
                                        value: '20'
                                    },
                                    {
                                        text: '30',
                                        value: '30'
                                    },
                                    {
                                        text: '40',
                                        value: '40'
                                    },
                                ],
                                defaultIndex: 3
                            },
                            {
                                values: ['分钟']
                            },]
                        }
                        if (ele.id == 3) {
                            ele.icon = 'https://pcsys.admin.ybc365.com/df9dc597-ddd7-464a-bbfd-8427c3b69d3c.png'
                            ele.columns = []
                            ele.columns2 = []
                        }
                    })
                    this.setData({
                        data: result.data.records
                    })
                }
            },
            (err) => {
                console.log(err);
            },
        )

    },
    /** 添加孩子 */
    addChild() {
        this.setData({
            showPopup: true
        })
    },
    onClosePopup() {
        this.setData({
            showPopup: false,
            errorFlag: false,
            errorIdCardFlag: false,
        })
    },
    /** 提交添加孩子 */
    submitAddFun() {
        if (this.data.name.trim() == '') {
            this.setData({
                errorFlag: true
            })
            return
        }
        if (this.data.idCard.trim() == '') {
            this.setData({
                errorIdCardFlag: true
            })
            return
        }
        const sfzReg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
        if (!sfzReg.test(this.data.idCard)) {
            wx.showToast({
                icon: "error",
                title: '请输入正确的身份证',
            })
            return
        }
        http.postHttp(api.addChild, {
                name: this.data.name,
                personal_id: this.data.idCard,
                user_id: this.data.user_id
            },
            (result) => {
                // this.setData({
                //     showPopup: false
                // })
                wx.setStorageSync('child', result.data)
                if (result.data.length > 0) {
                    wx.setStorageSync('childInfo', result.data[0])
                } else {
                    wx.setStorageSync('childInfo', {})
                }
                this.init()
            },
            (err) => {
                console.log(err);
            },
        )
    },
    inputFocusFun() {
        this.setData({
            errorFlag: false,
            errorIdCardFlag: false
        })
    },
    init() {
        const child = wx.getStorageSync('child')
        const childInfo = wx.getStorageSync('childInfo')
        const userInfo = wx.getStorageSync('userInfo')
        this.setData({
            timer: this.getNowTime(),
            child_id: childInfo.id ? childInfo.id : 0,
            childInfo: childInfo,
            child: child,
            user_id: userInfo.id,
            telephone:userInfo.telephone,
            radioId: childInfo.id
        })
        this.getSignRecord(this.data.child_id)
        if (this.data.idCard) {
            /** 添加孩子的时候检测有没有买产品，买了产品强制去激活权益 */
            http.postHttp(api.checkVIP, {
                personal_id:this.data.idCard,
                name:this.data.name
            },
                (result) => {
                    if (result.code == 0) {
                        if (result.data.if_vip) {
                            wx.navigateTo({
                                url: '/pages/activationInfo/index?childname=' + this.data.name +
                                    '&idcard=' + this.data.idCard + '&phone=' + this.data.telephone +
                                    '&from=my',
                            })
                        } else {
                            this.setData({
                                showPopup: false
                            })
                        }
                    }

                },
                (err) => {
                    console.log(err);
                },
            )
        }
    },
    /** 切换孩子 */
    changeChildFun() {
        this.setData({
            showSelectPopup: true
        })
    },
    /** 选择孩子触发 */
    onChangeRadio(e) {
        this.setData({
            radioId: e.detail.detail,
        });
    },
    selectChildFun2() {
        this.setData({
            showSelectPopup: false
        })
        setTimeout(() => {
            this.init()
        }, 500);
    },
    selectChildFun() {
        this.data.child.forEach(ele => {
            if (this.data.radioId == ele.id) {
                wx.setStorageSync('childInfo', ele)
                this.init()
                this.setData({
                    showSelectPopup: false
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (typeof this.getTabBar === 'function' &&
            this.getTabBar()) {
            this.getTabBar().setData({
                selected: 2 // 选中的tabBar
            })
        }
        this.init()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        this.setData({
            idCard:"",
            name:""
        })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})